<template>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">添加信息</h2>
    <div class="form-group">
      <label for>姓名</label>
      <input type="text" class="form-control" placeholder="请输入姓名" v-model="student.name" />
    </div>
    <div class="form-group">
      <label for>性别</label>
      <div>
        <label class="radio-inline">
          <input type="radio" :checked="true" name="gender" value="0" v-model="student.gender" /> 男
        </label>
        <label class="radio-inline">
          <input type="radio" name="gender" value="1" v-model="student.gender" /> 女
        </label>
      </div>
    </div>
    <div class="form-group">
      <label for>年龄</label>
      <input class="form-control" type="number" placeholder="请输入年龄" v-model="student.age" />
    </div>
    <div class="form-group">
      <label for>爱好</label>
      <input
        class="form-control"
        type="text"
        v-model="student.hobbies"
        id
        name="hobbies"
        placeholder="请输入爱好"
      />
    </div>
    <button @click="doSubmit" class="btn btn-primary">提交</button>
    <router-link class="btn btn-danger" to="/students">取消</router-link>
  </div>
</template>
<script>
import { Toast } from "mint-ui";
import { addStudentData } from "@/apis/data";
export default {
  data() {
    return {
      student: { gender: 0 }
    };
  },
  methods: {
  
    doSubmit(e) {
      addStudentData(this.student)
        .then(res => {
          var code = res.data;
          if (code.err_code == 0) {
            Toast({
              message: "添加信息成功！",
              position: "middle",
              duration: 1000,
              iconClass: "glyphicon glyphicon-ok"
            });
            this.$router.push("/students");
          } else if (code.err_code == 1) {
            Toast({
              message: "姓名不能为空！",
              position: "middle",
              duration: 1000,
              iconClass: "glyphicon glyphicon-remove"
            });
          } else if (code.err_code == 2) {
            Toast({
              message: "爱好不能为空！",
              position: "middle",
              duration: 1000,
              iconClass: "glyphicon glyphicon-remove"
            });
          } else if (code.err_code == 3) {
            Toast({
              message: "年龄不能为空！",
              position: "middle",
              duration: 1000,
              iconClass: "glyphicon glyphicon-remove"
            });
          } else if (code.err_code == 4) {
            Toast({
              message: "性别错误！",
              position: "middle",
              duration: 1000,
              iconClass: "glyphicon glyphicon-remove"
            });
          } else if (code.err_code == 5) {
            Toast({
              message: "姓名长度必须为2到10位",
              position: "middle",
              duration: 1000,
              iconClass: "glyphicon glyphicon-remove"
            });
          } else if (code.err_code == 6) {
            Toast({
              message: "年龄必须为数字",
              position: "middle",
              duration: 1000,
              iconClass: "glyphicon glyphicon-remove"
            });
          } else if (code.err_code == 7) {
            Toast({
              message: "年龄必须为0到150岁之间",
              position: "middle",
              duration: 1000,
              iconClass: "glyphicon glyphicon-remove"
            });
          } else if (code.err_code == 8) {
            Toast({
              message: "爱好不能少于四位",
              position: "middle",
              duration: 1000,
              iconClass: "glyphicon glyphicon-remove"
            });
          }
        })
        .catch(function() {
          Toast({
            message: "服务器异常",
            position: "middle",
            duration: 1000,
            iconClass: "glyphicon glyphicon-remove"
          });
        });
    }
  }
};
</script>
<style>
</style>